package session.stateless;

import entity.Address;
import entity.Employee;
import entity.Lead;
import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class LeadSessionBean implements LeadSessionBeanLocal, LeadSessionBeanRemote {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void addLead(String userId, String email, String firstName, String lastName, String company, String title, String status, Integer phone, Address companyAddress, Double revenue, String description, Integer numEmployee, String industry) {
        Employee employee = entityManager.find(Employee.class, userId);
        Address address = new Address(companyAddress.getStreet(), companyAddress.getCountry(), companyAddress.getCity(), companyAddress.getPostal(), "Company");
        Lead lead = new Lead(employee, email, firstName, lastName, company, title, status, phone, address, revenue, description, numEmployee, industry);
        entityManager.persist(lead);
        entityManager.flush();
    }

    @Override
    public Lead getLead(Long custId) {
        Lead lead = entityManager.find(Lead.class, custId);
        return lead;
    }

    @Override
    public ArrayList<Lead> getLeads() {
        Query query = entityManager.createQuery("SELECT u FROM Lead u WHERE u.status='Open'");
        ArrayList<Lead> al = new ArrayList<Lead>();
        for (Object o : query.getResultList()) {
            Lead l = (Lead) o;
            al.add(l);
        }
        return al;
    }

    @Override
    public void updateLead(Long custId, String userId, String email, String firstName, String lastName, String company, String title, String status, Integer phone, Address companyAddress, Double revenue, String description, Integer numEmployee, String industry) {
        try {
           

            Lead lead = entityManager.find(Lead.class, custId);
            if (lead != null) {
                Employee employee = entityManager.find(Employee.class, userId);
                lead.setEmployee(employee);
                lead.setEmail(email);
                lead.setFirstName(firstName);
                lead.setLastName(lastName);
                lead.setCompany(company);
                lead.setTitle(title);
                lead.setStatus(status);
                lead.setPhone(phone);
                Address address = new Address(companyAddress.getStreet(), companyAddress.getCountry(), companyAddress.getCity(), companyAddress.getPostal(), "Company");
                lead.setCompanyAddress(address);
                lead.setRevenue(revenue);
                lead.setDescription(description);
                lead.setNumEmployee(numEmployee);
                lead.setIndustry(industry);

                entityManager.persist(lead);
                entityManager.flush();
            }
           

        } catch (Exception ex) {
     
        }
    
    }

    @Override
    public void deleteLead(Long custId) {
        Lead lead = getLead(custId);
        if (lead != null) {
            Address address = entityManager.find(Address.class, lead.getCompanyAddress().getId());
            entityManager.remove(lead);
            entityManager.flush();
            entityManager.remove(address);
            entityManager.flush();
        }
    }

    @Override
    public Lead getLead(String email) {
        Query query = entityManager.createQuery("SELECT u FROM Lead u WHERE u.email = :inemail");
        query.setParameter("inemail", email);
        Lead lead = null;
        
        try {
            lead = (Lead) query.getSingleResult();
        } catch (Exception ex) {
        }
        return lead;

    }
}
